package com.weslide.weslideapp.fragment;

import org.json.JSONException;

import com.gt.cl.http.CLConnectionException;
import com.gt.cl.http.CLInvalidNetworkException;
import com.weslide.weslideapp.R;
import com.weslide.weslideapp.WeSlideApplication;
import com.weslide.weslideapp.activity.ExchangeSuccessActivity;
import com.weslide.weslideapp.activity.GiftDetailActivity;
import com.weslide.weslideapp.manager.api.APIManager;
import com.weslide.weslideapp.manager.api.LoginManager;
import com.weslide.weslideapp.pojo.ao.ExchangeScoreAO;
import com.weslide.weslideapp.pojo.ao.LoginAO;
import com.weslide.weslideapp.util.TMTPAsyncTask;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FillExchangeFormFragment extends WeSlideFragment {
	public static final String TAG = "FillExchangeFormFragment";
	private ImageView mImgVBack;
	private TextView mTvGoodsName;
	private TextView mTvScorePrice;
	private TextView mTvTotalNeedScore;
	private TextView mTvMyScore;
	private TextView mTvExchangeCount;
	private View mTvMinus;
	private View mTvPlus;
	private EditText mEdtReceiverName;
	private EditText mEdtReceiverAddress;
	private EditText mEdtReceiverPhone;
	private CheckBox mChxSaveInformation;
	private Button mBtnCommit;

	private String goodId;
	private String goodName;
	private int needScore;
	private int totalNeedScore;
	private String myScore;
	private String price;
	private int currentCount = 1;

	private TMTPAsyncTask<Void, Void, Void> asyncTask;

	private ExchangeScoreAO exchangeScoreAO;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		globalView = inflater.inflate(R.layout.page_fill_exchange_from,
				container, false);
		findViews();
		init();
		setListener();
		return globalView;
	}

	private void findViews() {
		mImgVBack = (ImageView) findViewById(R.id.page_fill_exchange_from_mImgVBack);
		mTvGoodsName = (TextView) findViewById(R.id.page_fill_exchange_from_mTvGoodsName);
		mTvScorePrice = (TextView) findViewById(R.id.page_fill_exchange_from_mTvScorePrice);
		mTvTotalNeedScore = (TextView) findViewById(R.id.page_fill_exchange_from_mTvTotalNeedScore);
		mTvMyScore = (TextView) findViewById(R.id.page_fill_exchange_from_mTvMyScore);
		mTvExchangeCount = (TextView) findViewById(R.id.page_fill_exchange_from_mTvExchangeCount);
		mTvMinus = findViewById(R.id.page_fill_exchange_from_mTvMinus);
		mTvPlus = findViewById(R.id.page_fill_exchange_from_mTvPlus);
		mEdtReceiverName = (EditText) findViewById(R.id.page_fill_exchange_from_mEdtReceiverName);
		mEdtReceiverAddress = (EditText) findViewById(R.id.page_fill_exchange_from_mEdtReceiverAddress);
		mEdtReceiverPhone = (EditText) findViewById(R.id.page_fill_exchange_from_mEdtReceiverPhone);
		mBtnCommit = (Button) findViewById(R.id.page_fill_exchange_from_mBtnCommit);
		mChxSaveInformation = (CheckBox) findViewById(R.id.page_fill_exchange_from_mChxSaveInformation);
	}

	private void init() {
		String receiverName = LoginManager.getInstance().getReceiverName();
		if (receiverName != null && !receiverName.equals("")) {
			mEdtReceiverName.setText(receiverName);
			String receiverAddress = LoginManager.getInstance()
					.getReceiverAddress();
			String receiverPhone = LoginManager.getInstance()
					.getReceiverPhone();
			mEdtReceiverPhone.setText(receiverPhone);
			mEdtReceiverAddress.setText(receiverAddress);
		}
		Bundle bundle = getArguments();
		price = bundle.getString("price");
		goodId = bundle.getString(GiftDetailActivity.EXTRA_GOODID);
		goodName = bundle.getString("goodName");
		needScore = Integer.parseInt(bundle.getString("needScore"));
		myScore = bundle.getString("myscore");
		totalNeedScore = needScore * currentCount;
		mTvGoodsName.setText(goodName);
		mTvScorePrice.setText(needScore + "积分");
		mTvTotalNeedScore.setText(totalNeedScore + "积分");
		mTvMyScore.setText(myScore);
	}

	private void setListener() {
		mImgVBack.setOnClickListener(myOnClickListener);
		mTvMinus.setOnClickListener(myOnClickListener);
		mTvPlus.setOnClickListener(myOnClickListener);
		mBtnCommit.setOnClickListener(myOnClickListener);
	}

	private OnClickListener myOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.page_fill_exchange_from_mTvPlus:
				plus();
				break;
			case R.id.page_fill_exchange_from_mTvMinus:
				minus();
				break;
			case R.id.page_fill_exchange_from_mBtnCommit:
				commit();
				break;
			case R.id.page_fill_exchange_from_mImgVBack:
				getActivity().finish();
				break;
			}
		}
	};

	private void plus() {
		currentCount++;
		totalNeedScore = currentCount * needScore;
		mTvTotalNeedScore.setText("" + totalNeedScore);
		mTvExchangeCount.setText("" + currentCount);
	}

	private void minus() {
		if (currentCount == 0) {
			return;
		}
		currentCount--;
		totalNeedScore = currentCount * needScore;
		mTvTotalNeedScore.setText("" + totalNeedScore);
		mTvExchangeCount.setText("" + currentCount);
	}

	private void commit() {
		asyncTask = new TMTPAsyncTask<Void, Void, Void>(getActivity()) {

			@Override
			protected void doOnSuccess(Void result) {

				if (exchangeScoreAO.getStatus().equals("1")) {
					Intent intent = new Intent();
					intent.setClass(getActivity(),
							ExchangeSuccessActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("seId", exchangeScoreAO.getSeId());
					bundle.putString("goodName", goodName);
					intent.putExtras(bundle);
					mTvMyScore
							.setText((Integer.parseInt(myScore) - totalNeedScore)
									+ "");
					Log.i(TAG, "myScore>>> "
							+ (Integer.parseInt(myScore) - totalNeedScore));
					LoginManager.getInstance().updateScore(
							(Integer.parseInt(myScore) - totalNeedScore) + "");
					float parseFloat = Float.parseFloat(price);
					LoginManager.getInstance().updateSaveMoney(
							"" + currentCount * parseFloat);
					startActivity(intent);
				} else {
					Toast.makeText(getActivity(), "兑换失败", Toast.LENGTH_SHORT)
							.show();
				}
			}

			@Override
			protected Void doInBackground(Void params)
					throws CLInvalidNetworkException, CLConnectionException {
				String phone = mEdtReceiverPhone.getText().toString();
				String address = mEdtReceiverAddress.getText().toString();
				String realName = mEdtReceiverName.getText().toString();
				if (mChxSaveInformation.isChecked()) {
					LoginManager.getInstance().saveAddressInformation(realName,
							address, phone);
				}
				try {
					LoginAO userInformation = LoginManager.getInstance()
							.getUserInformation();
					exchangeScoreAO = APIManager.getInstance().exchangeScore(
							userInformation.getUserId(), goodId, phone,
							address, realName, currentCount);
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return null;
			}
		};

		asyncTask.execute();
	}
}
